//
//  TestForNotificationForIpadVC.m
//  RoadMaintain
//
//  Created by 岳学坤 on 2017/3/17.
//  Copyright © 2017年 岳学坤. All rights reserved.
//

#import "TestForNotificationForIpadVC.h"
#import "SearchForDiseaseListIPadCell.h"
#import "PutNormolForIPadCell.h"

#define ListCellID @"cellid1"
#define PutCellID  @"cellid2"

@interface TestForNotificationForIpadVC ()
{
    NSInteger _selectedIndex;//选中查看第几个
    BOOL    _isAddState;//1，添加 0，查看
}
@property (nonatomic) NSInteger state;//0未上报  1已上报
@property (nonatomic,strong) NSArray *titleArray;
@property (nonatomic,strong) NSArray *contentArray;
@property (nonatomic,strong) UIView *rightFooterView;
@property (nonatomic,strong) UIButton *leftBtn;
@property (nonatomic,strong) UIButton *rightBtn;
@property (nonatomic,strong) NSDictionary *dic;//获取的需要展示的ID字典
@property (nonatomic,strong) NSString *codeId;
@property (nonatomic,strong) NSString *time;
@end

@implementation TestForNotificationForIpadVC

#pragma mark - Life cycle
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self resetNav];
    [self setTableViewFooterView];
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    [self setWebData];
}
#pragma mark - Property
- (NSArray *)titleArray{
    if (_titleArray==nil) {
        _titleArray = @[@"验收通知单编码",@"通知单编码",@"验收发起部门",@"验收接受部门",@"验收内容",@"验收人",@"验收日期"];
    }
    return _titleArray;
}
- (NSArray *)contentArray{
    if (_contentArray==nil) {
        _contentArray = @[@"超大工程",@"XCG76K100K300102030002",@"交建一局",@"厦门公路局",@"养护一队",@"2017-01-01",@"2017-02-02"];
    }
    return _contentArray;
}
#pragma mark - UI
- (void)resetNav{
    self.leftTitle = @"首页";
    self.pullRefreshEnable = YES;
    self.pullLoadMoreEnable = YES;
    self.myTitle.text = @"验收通知";
    [self.tableView registerClass:[SearchForDiseaseListIPadCell class] forCellReuseIdentifier:ListCellID];
    [self.rightTableView registerClass:[PutNormolForIPadCell class] forCellReuseIdentifier:PutCellID];
   
}
- (void)setTableViewFooterView{
  
    
    self.rightFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.rightTableView.frame.size.width, 212*PadHeightRate)];
    self.rightFooterView.backgroundColor = Bai_Color;
    self.rightTableView.tableFooterView = self.rightFooterView;
    
    self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.leftBtn.layer.cornerRadius = 5;
    self.leftBtn.layer.masksToBounds = YES;
    self.leftBtn.layer.borderColor = [MAIN_Color CGColor];
    self.leftBtn.layer.borderWidth = 1;
    self.leftBtn.backgroundColor = Bai_Color;
    [self.leftBtn setTitleColor:MAIN_Color forState:UIControlStateNormal];
    [self.leftBtn setTitle:@"重拟验收日期" forState:UIControlStateNormal];
    [self.leftBtn addTarget:self action:@selector(resetTime) forControlEvents:UIControlEventTouchUpInside];
    self.leftBtn.titleLabel.font = [UIFont systemFontOfSize:17*PadZitiRate];
    [self.rightFooterView addSubview:self.leftBtn];
    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-139*PadHeightRate);
        make.height.mas_equalTo(42*PadHeightRate);
        make.left.mas_equalTo(22*PadWidthRate);
        make.right.equalTo(self.rightFooterView.mas_centerX).offset(-15*PadWidthRate);
    }];
    
    
    self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightBtn.layer.cornerRadius = 5;
    self.rightBtn.layer.masksToBounds = YES;
    self.rightBtn.backgroundColor = MAIN_Color;
    [self.rightBtn setTitleColor:Bai_Color forState:UIControlStateNormal];
    [self.rightBtn setTitle:@"启动验收" forState:UIControlStateNormal];
    [self.rightBtn addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
    self.rightBtn.titleLabel.font = [UIFont systemFontOfSize:17*PadZitiRate];
    [self.rightFooterView addSubview:self.rightBtn];
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-139*PadHeightRate);
        make.height.mas_equalTo(42*PadHeightRate);
        make.right.mas_equalTo(-22*PadWidthRate);
        make.left.equalTo(self.rightFooterView.mas_centerX).offset(15*PadWidthRate);
    }];
}
#pragma mark - UI Response

- (void)resetTime{
    //x,y 值无效，默认是居中的
    KSDatePicker* picker = [[KSDatePicker alloc] initWithFrame:CGRectMake(0, 0, 280, 260)];
    
    //配置中心，详情见KSDatePikcerApperance
    
    picker.appearance.radius = 5;
    
    //设置回调
    picker.appearance.resultCallBack = ^void(KSDatePicker* datePicker,NSDate* currentDate,KSDatePickerButtonType buttonType){
        
        if (buttonType == KSDatePickerButtonCommit) {
            
            NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd"];
            self.time = [formatter stringFromDate:currentDate];
    
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            
            [NetWorkTool resetWithMaintentanceRecordId:self.dic[@"acceptanceNoticeId"] planAcceptanceDate:self.time block:^(id data) {
            [MBProgressHUD hideAllHUDsForView:self.view animated:NO];
                if (data) {
                    if ([data[@"success"]integerValue]) {
                        
                        
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"重拟验证日期成功" animated:YES];
                        
                        self.page = 1;
                        [self setWebData];
                        
                        
                    }else{
                        if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                            [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                            [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                        }else{
                            [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                        }                    }
                }else{
                    
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，操作失败。" animated:YES];
                }
            }];
            
        }
    };
    // 显示
    [picker show];
}
- (void)test{
    
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"确认" message:@"确定要启动验收么?" preferredStyle:UIAlertControllerStyleAlert];
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
        [NetWorkTool becomeReceiveWithmaintentanceRecordId:self.dic[@"acceptanceNoticeId"] block:^(id data) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:NO];
            if (data) {
                if ([data[@"success"]integerValue]) {
                    
                    
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"启动验收成功" animated:YES];
                    
                    
                    self.page = 1;
                    [self setWebData];
                    
                }else{
                    if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                        [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                    }else{
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                    }                }
            }else{
                
                [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，操作失败。" animated:YES];
            }
        }];

    }]];
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    
    [self presentViewController:actionSheet animated:YES completion:nil];

    
}
- (void)normolState:(NSInteger)state{
    if (state) {
        self.rightTableView.tableFooterView.hidden = YES;
    }else{
        self.rightTableView.tableFooterView.hidden = NO;
    }
    self.myCodeTitle.text = self.dataArray[_selectedIndex][@"acceptanceNoticeNo"];
}
#pragma mark - Data handle
- (void)setWebData{
    [NetWorkTool getAcceptanceNotificationWithPage:self.page pageSize:10 block:^(id data) {
        [self performSelector:@selector(MJRefreshStop) withObject:nil afterDelay:1];
        [MBProgressHUD hideAllHUDsForView:self.tableView animated:NO];
        if (data) {
            if ([data[@"success"] integerValue]) {
                if (self.page == 1) {
                    _selectedIndex = 0;
                    self.tableView.contentOffset = CGPointMake(0, 0);
                    [self.dataArray removeAllObjects];
                    if ([data[@"data"][@"items"]isKindOfClass:[NSArray class]]) {
                        [self.dataArray addObjectsFromArray:data[@"data"][@"items"]];
                    }
                }else{
                    if ([data[@"data"][@"items"]isKindOfClass:[NSArray class]]) {
                        [self.dataArray addObjectsFromArray:data[@"data"][@"items"]];
                    }
                }
                if ([data[@"data"][@"items"]isKindOfClass:[NSArray class]]) {
                    if ([data[@"data"][@"items"] count]<10&&(self.dataArray.count!=0)) {
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    }
                }
                if (self.dataArray.count == 0) {
                    self.noDataView.hidden = NO;
                    self.rightTableView.tableFooterView.hidden = YES;
                    self.myCodeTitle.text = nil;
                    self.dic = nil;
                    [self.rightTableView reloadData];
                }else{
                    self.noDataView.hidden = YES;
                    self.codeId = self.dataArray[0][@"acceptanceNoticeId"];
                    NSString *str = [NSString stringWithFormat:@"%@",self.dataArray[0][@"vcBooleanCode"]];
                    if ([str integerValue]==2) {
                        self.state = 1;
                    }else{
                        self.state = 0;
                    }
                    [self setWebDataDetail];
                    [self normolState:self.state];
                }
                [self.tableView reloadData];
                self.rightTableView.contentOffset = CGPointMake(0, 0);
            }else{
                if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                    [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                }else{
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                }
            }
            
        }else{
            [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，请检查网络后重试。" animated:YES
             ];
        }
    }];
}
- (void)gotoLogin{
    [PublicMethod noTokenToLoginWithNav:self.navigationController];
}
- (void)setWebDataDetail{
    self.rightTableView.contentOffset = CGPointMake(0, 0);
    [self.rightTableView reloadData];
    [MBProgressHUD showHUDAddedTo:self.rightTableView animated:YES];
    [NetWorkTool getAcceptanceNotificationDetailWithID:self.codeId block:^(id data) {
    [MBProgressHUD hideAllHUDsForView:self.rightTableView animated:NO];
        if (data) {
            if ([data[@"success"] integerValue]) {
                self.dic = data[@"data"];
                self.time = data[@"data"][@"planAcceptanceDate"];
                self.myCodeTitle.text = self.dataArray[_selectedIndex][@"acceptanceNoticeNo"];
                [self.rightTableView reloadData];
            }else{
                if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                    [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                }else{
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                }
            }
        }else{
            [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，请检查网络后重试。" animated:YES];
        }
    }];
}
#pragma mark - Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([tableView isEqual:self.tableView]) {
        return self.dataArray.count;
        
    }else{
        return 7;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.tableView]) {
        SearchForDiseaseListIPadCell *cell = (SearchForDiseaseListIPadCell *)[self tableView:self.tableView cellForRowAtIndexPath:indexPath];
        return cell.cellHeight;
    }else{
        if (indexPath.row==0) {
            return 68*PadHeightRate;
        }
        else{
            return 50*PadHeightRate;
        }
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.tableView]) {
        SearchForDiseaseListIPadCell *cell = [tableView dequeueReusableCellWithIdentifier:ListCellID];
        if (_selectedIndex == indexPath.row) {
            cell.contentView.backgroundColor = Bai_Color;
            
            cell.contentView.layer.borderWidth = 0;
            cell.blueLine.hidden = NO;
        }else{
            cell.contentView.backgroundColor = [PublicMethod colorWithHexString:@"#f4f4f4" andAlpha:1];
            cell.contentView.layer.borderColor = [Line_Color CGColor];
            cell.contentView.layer.borderWidth = 1;
            cell.blueLine.hidden = YES;
        }
        [cell acceptanceNotificationAddValue:self.dataArray[indexPath.row]];
        return cell;
    }else{
       
            PutNormolForIPadCell *cell = [tableView dequeueReusableCellWithIdentifier:PutCellID];
            cell.titleLab.text = self.titleArray[indexPath.row];
        switch (indexPath.row) {
            case 0:
            {
                cell.contentLab.text = self.dic[@"acceptanceNoticeNo"];
                [cell.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.centerY.mas_equalTo(0);
                    make.height.mas_equalTo(36*PadHeightRate);
                    make.left.mas_equalTo(12*PadWidthRate);
                    make.width.mas_equalTo(95*PadWidthRate);
                }];
            }
                break;
            case 1:
            {
                cell.contentLab.text = self.dic[@"maintentanceNoticeNo"];
            }
                break;
            case 2:
            {
                cell.contentLab.text = self.dic[@"lanuchDepartmentName"];
            }
                break;
            case 3:
            {
                cell.contentLab.text = self.dic[@"receiveDepartmentName"];
            }
                break;
            case 4:
            {
                cell.contentLab.text = self.dic[@"content"];
            }
                break;
            case 5:
            {
                cell.contentLab.text = self.dic[@"acceptanceUserName"];
            }
                break;
            case 6:
            {
                cell.contentLab.text = self.dic[@"acceptanceDate"];
            }
                break;
           
            default:
                break;
        }
            cell.img.hidden = YES;
            return cell;
        
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.tableView]) {
        _selectedIndex = indexPath.row;
        [self.tableView reloadData];
        self.codeId = self.dataArray[indexPath.row][@"acceptanceNoticeId"];
        NSString *str = [NSString stringWithFormat:@"%@",self.dataArray[indexPath.row][@"vcBooleanCode"]];
        if ([str integerValue]==2) {
            self.state = 1;
        }else{
            self.state = 0;
        }
        [self setWebDataDetail];
        [self normolState:self.state];
    }else{
        
    }
    
}

@end
